package main

import (
	"fmt"
	"go_chat_system/server/model"
	"go_chat_system/server/process"
	"go_chat_system/server/utils"
	"net"
	"time"
)

func main() {
	// 初始化Redis连接池
	utils.InitPool("101.37.247.163:6379", "admin", 8, 0, 100*time.Second)
	// 初始化AccountDao实例
	initAccountDao()

	fmt.Println("服务器启动...")

	listen, err := net.Listen("tcp", "0.0.0.0:8888")
	if err != nil {
		return
	}
	defer listen.Close()

	for {
		conn, err := listen.Accept()
		if err != nil {
			return
		}
		pro(conn)
	}
}

func pro(conn net.Conn) {
	// 创建一个安全的协程，调用一个客户端协程
	processor := &process.Processor{
		Conn: conn,
	}
	processor.CreateSafeGo(func(err interface{}) {
		fmt.Println("协程异常！")
	})
}

// 初始化AccountDao实例
func initAccountDao() {
	model.MyAccountDao = model.NewAccountDao(utils.Pool)
}
